<!--
 * @Description: 监测月报
 * @Author: Ronda
 * @Date: 2022-02-16 09:30:08
 * @LastEditors: 王恺天 2804952352@qq.com
 * @LastEditTime: 2025-09-26 11:28:52
-->
<!-- <template>
  <div>监测月报</div>
</template>

<script>
export default {
  data() {
    return {};
  },
  computed: {},
  methods: {},
  created() {},
  mounted() {},
};
</script>
<style scoped>
</style> -->
<template>
    <div>
        <el-row>
            <el-col :span="4">
                <div class="tree">
                    <el-input placeholder="输入关键字进行过滤" v-model="filterText">
                    </el-input>
                    <el-tree @node-click="handlerClick" class="filter-tree" :data="EngData" :props="defaultProps"
                        :filter-node-method="filterNode" ref="tree">
                    </el-tree>
                </div>
            </el-col>
            <el-col :span="20">
                <div v-if="params.device_id" class="table">
                    <el-form :inline="true" :model="form">
                        <el-form-item>
                            <!-- value-format="yyyy-MM" 2021-05格式 -->
                            <el-date-picker value-format="timestamp" v-model="form.time" type="month"
                                :picker-options="pickerOptions" placeholder="选择月" align="middle">
                                <!-- value-format="timestamp"
                            含义：将选择的日期值转换为时间戳格式
                            示例：选择 "2023-05" 会转换为 1682870400000 这样的13位时间戳
                            注意：对于月份范围，会返回包含开始月和结束月时间戳的数组 -->
                                <!-- v-model="form.time"
                            含义：双向数据绑定，将选择的值绑定到 form 对象的 time 属性上
                            数据类型：由于是 monthrange，会返回一个数组 [startTimestamp, endTimestamp] -->
                                <!-- type="monthrange"
                            含义：选择月份范围（而不是单个日期或日期时间）
                            效果：显示月份选择器，用户可以选择开始月份和结束月份
                            示例：可以选择 "2023-01" 到 "2023-12" -->

                            </el-date-picker>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="success" @click="toSearch" icon="src/icons/svg/搜索.svg">搜索</el-button>
                        </el-form-item>
                        <el-form-item style="float: right;">
                            <el-button type="success" @click="toSearch"  icon="">下载</el-button>
                        </el-form-item>
                        <!-- <el-form-item>
                            <el-button type="success" @click="toSearch" align="right">下载</el-button>
                        </el-form-item> -->
                    </el-form>
                    <!-- 表格  -->
                    <el-table :data="monitorData" style="width: 100%" :row-class-name="tableRowClassName">
                        <el-table-column prop="insert_time" label="日期" width="180" align="center">
                            <template v-slot="scope">
                                <div>
                                    {{ scope.row.insert_time | fmtDate }}
                                </div>
                            </template>

                        </el-table-column>
                        <el-table-column prop="tsp" label="tsp">
                        </el-table-column>
                        <el-table-column prop="pm10" label="pm10">
                        </el-table-column>
                        <el-table-column prop="pm25" label="pm2.5">
                        </el-table-column>
                        <el-table-column prop="temperature" label="温度">
                        </el-table-column>
                        <el-table-column prop="humidity" label="湿度">
                        </el-table-column>
                        <el-table-column prop="wind_direction" label="风向" width="300">
                        </el-table-column>
                        <el-table-column prop="wind_direction" label="风速" width="300">
                        </el-table-column>

                    </el-table>
                </div>
                <div v-else style="line-height: 50px;color: red;font-size: 20px;text-align: center;">请点击设备查看数据</div>
            </el-col>
        </el-row>
    </div>
</template>
<script>
import { get } from '@/utils/request'
export default {
    data() {
        return {
            filterText: '',
            defaultProps: {
                children: 'children',
                label: 'name'
            },
            // 表格数据源 
            monitorData: [],
            // 表单数据对象 
            form: { time: [] },
            // 树形结构展示数据 
            EngData: [],
            // 快捷选项配置项 

            params: {
                page: 1,
                pageSize: 3,
                device_id: 0,
                engineer_id: 0
            }
        }
    },
    watch: {
        // 监听用户输入框输入的值并进行过滤 
        filterText(val) {
            this.$refs.tree.filter(val);
        }
    },
    methods: {
        // 点击节点触发 
        handlerClick(val, node) {
            // console.log(val);
            // 判断点击的是设备  将设备id 工程id page pageSize传给后端 
            if (!val.children) {
                this.params.device_id = val.id;
                this.params.engineer_id = val.engineer_id;
                this.getMonitor();
            }
        },
        // 搜索 
        toSearch() {
            this.params.page = 1;
            this.getMonitor();
        },
        // 封装方法获取监测历史数据
        async getMonitor() {
            let temp = {
                ...this.params
            };
            // 如果传了时间  
            if (this.form.time) {
                temp.begin_time = this.form.time[0],
                    temp.end_time = this.form.time[1]
            }
            let res = await get('/monitor/pageQuery', temp);
            console.log(res, '-------');
            this.monitorData = res.data.list;
        },
        // getData(){
        //     // axios 基于promise的http客户端 
        //     get().then((res)=>{
        //         // 处理业务逻辑 
        //         this.xxx = res.data;
        //     })
        // },
        // 封装方法获取工程设备树 
        async getEngDevData() {
            let res = await get('/engineer/findEngineerDeviceTree');
            console.log(res, '88888');
            this.EngData = res.data;
        },
        filterNode(value, data) {
            // data是树形结构数据 
            if (!value) return true;
            // value输入框输入的值 
            // 返回就是包含输入框关键字的label(节点)  
            return data.name.indexOf(value) !== -1;
        },
        tableRowClassName({ row, rowIndex }) {
            // console.log(rowIndex)
            if (rowIndex % 2 == 0 || rowIndex == 0) {
                return 'warning-row';
            } else {
                return 'success-row';
            }
            return '';
        }
    },
    created() {
        this.getEngDevData();
        this.getMonitor();
    }
}
</script>
<style lang="scss" scoped>
.tree {
    height: calc(100vh - 60px);
    padding-right: 10px;
    border-right: 1px solid #ececec;
    margin-right: 10px;
}

::v-deep .el-table .warning-row {
    background: whitesmoke;
}

::v-deep .el-table .success-row {
    background: white;
}
</style>